package com.itheima.controller;


import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import com.sun.xml.bind.v2.TODO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-07-30 18:12
 **/
@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询
     *
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result queryClazzPageList(ClazzQueryParam clazzQueryParam) {
        log.info("查询全部班级");
        PageResult<Clazz> pageResult = clazzService.queryClazzPageList(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 查询全部班级
     *
     * @return
     */
    @GetMapping("/list")
    public Result queryClazzList() {
        log.info("查询全部班级");
        List<Clazz> clazzList = clazzService.queryClazzList();
        return Result.success(clazzList);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id){
        Clazz clazz = clazzService.getClazzById(id);
        return Result.success(clazz);
    }
    /**
     * 添加班级信息
     *
     * @param clazz
     * @return
     */
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        log.info("添加班级信息");
        Integer num = clazzService.addClazz(clazz);
        if (num >= 1) {
            log.info("添加班级信息成功！");
            return Result.success();
        } else
            return Result.error("添加班级信息失败！");
    }

    /**
     * 根据id删除班级
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delClazzById(@PathVariable("id") Integer id) {
        Integer count = clazzService.delClazzById(id);
        if (count == 0) {
            return Result.success();
        } else
            return Result.error("对不起，该班级下面有学生,不能直接删除");
    }

    /**
     * 修改班级信息
     * @param clazz
     * @return
     */
    @PutMapping
    public Result updateClazzById(@RequestBody Clazz clazz){
        log.info("修改班级" + clazz);
        clazzService.updateClazzById(clazz);
        return Result.success();
    }

}
